/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package earthdawnnet.client;

import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InvalidClassException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

/**
 *
 * @author Andreas Franek
 */
public class Persister <T extends Serializable>
{
    public void save(T t, String filename)
    {
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;
        try
        {
            fos = new FileOutputStream(filename);
            oos = new ObjectOutputStream(fos);
            oos.writeObject(t);
        } catch (IOException ex)
        {
            //Logger.getLogger(Persister.class.getName()).log(Level.SEVERE, null, ex);
        } finally
        {
            try
            {
                if(oos != null) oos.close();
                if(fos != null) fos.close();
            } catch (IOException ex)
            {
                //Logger.getLogger(Persister.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public T load(String filename)
    {
        T result = null;
        FileInputStream fis = null;
        ObjectInputStream ois = null;
        try
        {
            fis = new FileInputStream(filename);
            ois = new ObjectInputStream(fis);
            result = (T) ois.readObject();
        } catch (EOFException ex)
        {
            //Logger.getLogger(Persister.class.getName()).log(Level.INFO, null, "File could not be loaded");
        }catch (InvalidClassException ex)
        {
            //Logger.getLogger(Persister.class.getName()).log(Level.INFO, null, "File could not be loaded");
        }catch (ClassNotFoundException ex)
        {
            //Logger.getLogger(Persister.class.getName()).log(Level.INFO, null, "File could not be loaded");
        } catch (IOException ex)
        {
            //Logger.getLogger(Persister.class.getName()).log(Level.INFO, null, "File could not be loaded");
        } finally
        {
            try
            {
                if(ois != null) ois.close();
                if(fis != null) fis.close();
            } catch (IOException ex)
            {
                //Logger.getLogger(Persister.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return result;
    }

}
